#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

/*次线程的个数*/
#define SECON_PTH_NUMS 2

/*线程的参数结构体*/
typedef struct pthread_arg{
	int fd;			/*线程操作的文件描述符*/
	int pthno;		/*自定义的线程编号*/
	pthread_t tid;	/*线程ID*/
}ptharg;

/*报错函数*/
void print_err(char *str, int line, int err_no)
{
	printf("%d,%s:%s",line,str,strerror(err_no));
	exit(-1);
}

/*线程函数*/
void * pth_fun(void *pth_arg)
{
	int fd = ((ptharg *)(pth_arg))->fd;
	pthread_t tid = ((ptharg *)pth_arg)->tid;
	int pthno = ((ptharg *)pth_arg)->pthno;

	printf("pthno=%d , tid =%lu\n",pthno,tid );

	while(1)
	{
		write(fd,"hello ",6);
		write(fd,"world\n",6);
	}
	return NULL;
} 


int main(void)
{
	int ret = 0;
	int i = 0;
	int fd =0;
	ptharg pthargs[SECON_PTH_NUMS];/*线程参数*/

	fd = open("./file",O_RDWR|O_CREAT|O_TRUNC,0664);
	if(fd == -1) print_err("open ",__LINE__,fd);
	
	for(i=0;i<SECON_PTH_NUMS;i++)
	{
		pthargs[i].fd = fd;
		pthargs[i].pthno = i;

		ret = pthread_create(&pthargs[i].tid,NULL,pth_fun,&pthargs[i]);
		if(ret != 0) print_err("pthread_create",__LINE__,ret);
	}
	
	
	while(1)
	{
		write(fd,"hello ",6);
		write(fd,"world\n",6);
	}
	
	return 0;
}






































